<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Userdata; 
use app\model\Lottery; 

class LtInfo extends Base
{
    public function Lt_info()
    {
/**
 * Created by 易宣
 * Desc:获取抽奖奖项以及奖项数量 返回用户的积分和剩余抽奖次数
 * User:cbw
 * Date: 2020/11/28
 */
global $_P, $_G;
$response = modelShow(40000);
if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $fans = $this->checkuser();
    $system=$_G['system'];
    $params = $_P;
    $themeid = isset($params['themeid'])?$params['themeid']:'';
    $is_max_chance = false;//达到最大抽奖上限
    if (!$themeid || empty($fans) || empty($system)){
        $response = modelShow(40001);
    }else{
        try {
            $theme = Theme::getThemeById($themeid);
            $config =   Theme::getConfig($themeid);
            $chance_limit = $config['fanpailimit'];
            if (!$theme){
                $response = modelShow(40002);
                wxapp_result(0, '',$response);
            }
            if (!$config){
                wxapp_result(0, '',$response);
            }
            //通过fans信息获取粉丝的积分和抽奖次数 更新抽奖机会 以及用户剩余抽奖机会
            $pointsAndCounter = Lottery::checkUserLtNum($fans,$config,$themeid);
            $addRecordsList = getAddRecordTypeList();//增加抽奖方式的list
            $has_free = Lottery::getLtItemTotal($themeid,2)>0?true:false;
            unset($addRecordsList[1]);
            if ($chance_limit && $pointsAndCounter['lt_counter']>=$chance_limit){
                $is_max_chance = true;
            }
            $sql = " SELECT * from " . tablename(t_lottery) ." WHERE themeid=" . $themeid.
                " order by  sort desc ,ltt_ratiio asc";
            $list = db_select($sql);
            $lt_total = count($list);
            $real_flag = Lottery::getUserRecords(0,$themeid,3);
            $return_real_flag = $config['fakelottery']+$real_flag;
            //获取最近的中间记录 真实+虚拟
            $record_list = Lottery::getVirtualFlag($themeid,$config);
            //获取用户的中奖记录
            $user_record_list = Lottery::getUserRecordListByUid($themeid,$fans['uid']);
            $has_user_name_mobile = false;
            if (!empty($fans['username']) && !empty($fans['mobile'])){
                $has_user_name_mobile = true;
            }
            //用户日志收集
            Userdata::addLog($themeid, $fans['uid'], 20, '');

            $config_new = array(
                'lottery_mytips' => $config ['lottery_mytips'],
                'lottery_title' => $config ['lottery_title'],
                'lottery_join' => $config ['lottery_join'],
                'first_buy_tips'=> $config['first_buy_tips'],
                'address'=> $config['district'].$config['addresstxt'],
                'mobile'=>$theme['mobile'],
            );
            $return_data = [
                'config'=>$config_new,
                'list'=>$list,
                'list_total'=>$lt_total,
                'remnant_num'=>$pointsAndCounter['lt_counter'],
                'current_points'=>$pointsAndCounter['lt_points'],
                'addRecordsList'=>$addRecordsList,
                'is_max_chance'=>$is_max_chance,
                'virtual'=>$return_real_flag,
                'record_list'=>$record_list,//中奖记录
                'has_free'=>$has_free,
                'user_record_list'=>formatUserRecordList($user_record_list),
                'wechatgrouptips'=>$config['wechatgrouptips'],
                'has_user_name_mobile'=>$has_user_name_mobile,
            ];
            $response = modelShow(200,'',$return_data);
        }catch (Exception $e){
            logging_run('错误日志:GetLttInfoByThemeId[参数：'.json_encode($params).'-错误信息:'.$e->getMessage());
        }
    }

}
wxapp_result(0, '',$response);
}
}